package dfs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * https://leetcode-cn.com/problems/power-set-lcci/
 * 幂集。编写一种方法，返回某集合的所有子集。集合中不包含重复的元素。
 * 说明：解集不能包含重复的子集。
 *
 * @author: luo
 * @email: luo@nyist.edu.cn
 * @date: 2021/2/6 18:35
 */
public class Subsets2 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(subsets(nums));
    }

    public static List<List<Integer>> list = new LinkedList<>();

    public static List<List<Integer>> subsets(int[] nums) {
        backtracking(nums, new LinkedList<>(), 0);
        return list;
    }

    private static void backtracking(int[] nums, LinkedList<Integer> arry, int index) {
        list.add(new ArrayList<>(arry));
        for (int i = index; i < nums.length; i++) {
            if (i != 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            arry.addLast(nums[i]);
            backtracking(nums, arry, i + 1);
            arry.removeLast();
        }
    }

    /*public static List<List<Integer>> subsets(int[] nums) {
        Arrays.sort(nums);
        list.add(new ArrayList<Integer>());
        for (int i = 1; i <= nums.length; i++) {
            backtracking(i, nums, new LinkedList<Integer>(), 0);
        }

        return list;
    }

    private static void backtracking(int n, int[] nums, LinkedList<Integer> arry, int idex) {
        if (n == 0) {
            list.add(new ArrayList<>(arry));
        }
        if (n <= nums.length - idex) {
            n--;
            for (int i = idex; i < nums.length && nums.length - i >= n; i++) {
                if (i != 0 && nums[i] == nums[i - 1]) {
                    continue;
                }
                arry.addLast(nums[i]);
                backtracking(n, nums, arry, i + 1);
                arry.removeLast();
            }

        }

    }*/
}
